package com.triumphxx.readingreminderbackend.mapper;

import com.triumphxx.readingreminderbackend.entity.ReviewSchedule;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Optional;

/**
 * @author: wangyp
 * @Date: 2025/11/20
 * @Time: 17:01
 * @Desc:
 **/


public interface ReviewScheduleMapper {

    // 根据笔记ID查询复习计划
    @Select("SELECT * FROM review_schedules WHERE note_id = #{noteId} ORDER BY stage")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "stage", column = "stage"),
            @Result(property = "scheduledDate", column = "scheduled_date"),
            @Result(property = "completed", column = "completed"),
            @Result(property = "completedAt", column = "completed_at"),
            @Result(property = "noteId", column = "note_id")
    })
    List<ReviewSchedule> findByNoteId(Long noteId);

    // 根据笔记ID和阶段查询复习计划
    @Select("SELECT * FROM review_schedules WHERE note_id = #{noteId} AND stage = #{stage}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "stage", column = "stage"),
            @Result(property = "scheduledDate", column = "scheduled_date"),
            @Result(property = "completed", column = "completed"),
            @Result(property = "completedAt", column = "completed_at"),
            @Result(property = "noteId", column = "note_id")
    })
    Optional<ReviewSchedule> findByNoteIdAndStage(@Param("noteId") Long noteId, @Param("stage") Integer stage);

    // 插入复习计划
    @Insert("INSERT INTO review_schedules (stage, scheduled_date, completed, completed_at, note_id) " +
            "VALUES (#{stage}, #{scheduledDate}, #{completed}, #{completedAt}, #{noteId})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(ReviewSchedule reviewSchedule);

    // 批量插入复习计划
    @Insert("<script>" +
            "INSERT INTO review_schedules (stage, scheduled_date, completed, note_id) VALUES " +
            "<foreach collection='list' item='item' separator=','>" +
            "(#{item.stage}, #{item.scheduledDate}, #{item.completed}, #{item.noteId})" +
            "</foreach>" +
            "</script>")
    int batchInsert(@Param("list") List<ReviewSchedule> reviewSchedules);

    // 更新复习计划
    @Update("UPDATE review_schedules SET completed = #{completed}, completed_at = #{completedAt} " +
            "WHERE id = #{id}")
    int update(ReviewSchedule reviewSchedule);

    // 删除指定笔记的所有复习计划
    @Delete("DELETE FROM review_schedules WHERE note_id = #{noteId}")
    int deleteByNoteId(Long noteId);

    // 查询今天需要复习的所有计划
    @Select("SELECT rs.*, n.* FROM review_schedules rs " +
            "INNER JOIN notes n ON rs.note_id = n.id " +
            "WHERE rs.completed = 0 AND DATE(rs.scheduled_date) = CURDATE()")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "stage", column = "stage"),
            @Result(property = "scheduledDate", column = "scheduled_date"),
            @Result(property = "completed", column = "completed"),
            @Result(property = "completedAt", column = "completed_at"),
            @Result(property = "noteId", column = "note_id"),
            @Result(property = "note.id", column = "note_id"),
            @Result(property = "note.title", column = "title"),
            @Result(property = "note.yuqueUrl", column = "yuque_url"),
            @Result(property = "note.folder", column = "folder"),
            @Result(property = "note.createdAt", column = "created_at")
    })
    List<ReviewSchedule> findAllTodayReviews();
}
